package com.relytosoft.mizar.media.analyze.services.controller.system;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.relytosoft.dubhe.common.core.base.page.PageQuery;
import com.relytosoft.dubhe.common.core.object.ResponseResult;
import com.relytosoft.dubhe.common.core.utils.BeanCopyUtils;
import com.relytosoft.dubhe.common.core.utils.CommonUtils;
import com.relytosoft.dubhe.common.log.annotation.OpLog;
import com.relytosoft.dubhe.common.log.enums.OpTypeEnum;
import com.relytosoft.mizar.media.analyze.api.entity.system.AyGroupBase;
import com.relytosoft.mizar.media.analyze.api.pojo.dto.AyGroupBaseDto;
import com.relytosoft.mizar.media.analyze.api.pojo.vo.AyGroupBaseVo;
import com.relytosoft.mizar.media.analyze.services.service.system.AyGroupBaseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * SystemController
 *
 * @Author SunLi
 * @Date: 2022/5/19 11:53
 * @Version 1.0
 */
@RestController
@RequestMapping("analyze/system/group")
@Api(tags = "分析系统基础接口")
public class AyGroupBaseController {

    @Resource
    private AyGroupBaseService ayGroupBaseService;

    @SaCheckPermission("faceLibrary:faceType:create")
    @OpLog(msg = "新增库", opType = OpTypeEnum.INSERT)
    @ApiOperation("新增库")
    @PostMapping({"/add"})
    public ResponseResult<Void> add(@RequestBody AyGroupBaseDto ayGroupBaseDto) {
        AyGroupBase ayGroupBase = BeanCopyUtils.copy(ayGroupBaseDto, AyGroupBase.class);
        boolean b = ayGroupBaseService.addAyGroupBase(ayGroupBase);
        if (b) {
            return ResponseResult.ok("新增库成功");
        } else {
            return ResponseResult.fail("新增库失败");
        }
    }

    @SaCheckPermission("faceLibrary:faceType:list")
    @OpLog(msg = "库列表查询", opType = OpTypeEnum.SELECT)
    @ApiOperation("库列表查询")
    @GetMapping("/list")
    public ResponseResult<Map<String, Object>> list(AyGroupBaseDto ayGroupBaseDto, PageQuery pageQuery) {
        AyGroupBase ayGroupBase = BeanCopyUtils.copy(ayGroupBaseDto, AyGroupBase.class);
        IPage<AyGroupBase> page = ayGroupBaseService.selectAyGroupBasePageList(pageQuery, ayGroupBase);
        return ResponseResult.ok(CommonUtils.getPageData(page));
    }


    @OpLog(msg = "详情", opType = OpTypeEnum.SELECT)
    @ApiOperation("根据ID查询")
    @GetMapping({"{groupId}"})
    public ResponseResult<AyGroupBaseVo> id(@PathVariable Integer groupId) {
        return ResponseResult.ok(ayGroupBaseService.findById(groupId));
    }


    @SaCheckPermission("faceLibrary:faceType:delete")
    @OpLog(msg = "删除库", opType = OpTypeEnum.DELETE)
    @ApiOperation("删除库")
    @DeleteMapping("del")
    public ResponseResult<Void> remove(@RequestBody List<Integer> list) {
        ArrayList<String> plateformGroupIds = ayGroupBaseService.updateStatus(list);
        if (plateformGroupIds != null && !plateformGroupIds.isEmpty()) {
            ayGroupBaseService.sendRemoveToKafka(plateformGroupIds);
            return ResponseResult.ok("删除人脸库成功");
        }
        if (plateformGroupIds != null) {
            return ResponseResult.ok("删除车辆库成功");
        } else {
            return ResponseResult.fail("删除失败");
        }
    }

    @SaCheckPermission("faceLibrary:faceType:update")
    @OpLog(msg = "修改库", opType = OpTypeEnum.UPDATE)
    @ApiOperation("修改库")
    @PutMapping
    public ResponseResult<Void> update(@RequestBody AyGroupBaseDto ayGroupBaseDto) {
        AyGroupBase ayGroupBase = BeanCopyUtils.copy(ayGroupBaseDto, AyGroupBase.class);
        boolean b = ayGroupBaseService.updateAyGroupBase(ayGroupBase);
        if (b) {
            return ResponseResult.ok("修改成功");
        } else {
            return ResponseResult.fail("修改失败");
        }
    }
}
